package com.tuanzhang.mall.goods.domain.valueobj;

import com.tuanzhang.mall.seller.types.exception.ApiException;
import lombok.Getter;

import java.util.List;

@Getter
public class Sku {

    /**
     * sku编码
     */
    private SkuCode skuCode;



    /**
     * [{"id":1,"spec": "颜色","item": {"id":1,"value":"黄"}},{"id":1,"spec": "尺寸","item": {"value":"xxl"}}]
     */
    private List<Spec> spec;

    /**
     * 价格
     */
    private Money money;

    /**
     * 库存
     */
    private Inventory inventory;

    private Integer saleMinNum;

    private Sku(){

    }

    private Sku(SkuCode skuCode, List<Spec> spec, Money money, Integer saleMinNum, Inventory inventory){
        if (null == saleMinNum) {
            throw new ApiException("未设置起批量");
        }
        if (inventory.getQuantity().compareTo(saleMinNum) < 0) {
            throw new ApiException("商品库存不能小于起批量");
        }

        this.skuCode = skuCode;
        this.spec = spec;
        this.money = money;
        this.saleMinNum = saleMinNum;
        this.inventory = inventory;
    }

    public static Sku createSku(SkuCode skuCode, List<Spec> spec, Money money, Integer saleMinNum, Inventory inventory) {
        return new Sku(skuCode, spec, money, saleMinNum, inventory);
    }
}
